package com.happycat.controller.social;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.happycat.base.PageParams;
import com.happycat.base.R;
import com.happycat.social.dto.SocialVoiceRoomQueryDto;
import com.happycat.social.dto.SocialVoiceRoomSaveDto;
import com.happycat.social.dto.SocialVoiceRoomUpdateDto;
import com.happycat.social.entity.SocialVoiceRoom;
import com.happycat.social.service.ISocialVoiceRoomService;
import com.happycat.social.vo.SocialVoiceRoomUserRoleVo;
import com.happycat.utils.ObjectsUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * @Author shenbin
 * @CreatedDate 2020-09-10
 * @Modified By:
 */
@RestController
@RequestMapping("/social/room")
@Api(tags = "API-聊天室-房间管理", value = "聊天室")
public class SocialRoomController {
    @Autowired
    private ISocialVoiceRoomService socialVoiceRoomService;

    @ApiOperation(value = "申请/创建房间", notes = "支持个人和官方创建，为方便测试，目前无需审批，直接申请后可用，后续改成后台审批")
    @PostMapping(value = "/applyRoom")
    public R<SocialVoiceRoom> applyRoom(@RequestBody @Valid SocialVoiceRoomSaveDto socialVoiceRoomSaveDto){
        return R.success(socialVoiceRoomService.applyRoom(socialVoiceRoomSaveDto));
    }

    @ApiOperation(value = "修改房间资料", notes = "可操作权限：房主; 请务必回传不修改的字段，否则将更新为空")
    @PostMapping(value = "/modifyRoom")
    public R<SocialVoiceRoom> modifyRoom(@RequestBody @Valid SocialVoiceRoomUpdateDto socialVoiceRoomUpdateDto){
        return R.success(socialVoiceRoomService.modifyRoom(socialVoiceRoomUpdateDto));
    }

    @ApiOperation(value = "查询房间详情")
    @GetMapping(value = "/{id}/get")
    public R<SocialVoiceRoom> getRoom(@PathVariable @NotNull Long id){
        ObjectsUtil.requireNonNull(id, "房间不能为空");

        return R.success(socialVoiceRoomService.getById(id));
    }

    @ApiOperation(value = "查询房间列表", notes = "只可查询审批通过的，有效的，可见的房间")
    @PostMapping(value = "/pageList")
    public R<IPage<SocialVoiceRoom>> pageList(@RequestBody @Valid PageParams<SocialVoiceRoomQueryDto> socialVoiceRoomQueryDto){
        IPage<SocialVoiceRoom> page = socialVoiceRoomQueryDto.buildPage();

        SocialVoiceRoom roomWs = BeanUtil.copyProperties(socialVoiceRoomQueryDto.getModel(), SocialVoiceRoom.class);
        roomWs.setVisible("1");
        roomWs.setAuditStatus("1");
        roomWs.setValid("1");
        return R.success(socialVoiceRoomService.page(page, new QueryWrapper<>(roomWs)));
    }

    @ApiOperation(value = "关闭/打开房间", notes = "可操作权限：房主")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "房间主键id", required = true),
        @ApiImplicitParam(name = "open", value = "1:打开，2关闭", required = true)
    })
    @PostMapping(value = "/closeOrOpenRoom")
    public R<Boolean> closeRoom(@RequestParam @NotNull Long id,
                                @RequestParam @NotNull @Range(min = 1, max = 2, message = "非法参数值") Integer open){
        ObjectsUtil.requireNonNull(id, "房间不能为空");
        ObjectsUtil.requireNonNull(open, "动作不能为空");

        return R.success(socialVoiceRoomService.closeOrOpenRoom(id, open));
    }

    @ApiOperation(value = "设置/取消主持", notes = "可操作权限：房主，取消其它用户主持身份时，房主将变成主持, 主持将拥有管理员权限")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "房间主键id", required = true),
        @ApiImplicitParam(name = "open", value = "1:设置为主持，2取消", required = true),
        @ApiImplicitParam(name = "accountId", value = "被操作平台用户id", required = true)
    })
    @PostMapping(value = "/operateHostess")
    public R<Boolean> operateHostess(@RequestParam @NotNull Long id,
                                     @RequestParam @NotNull Long accountId,
                                     @RequestParam @NotNull @Range(min = 1, max = 2, message = "非法参数值") Integer open){
        ObjectsUtil.requireNonNull(id, "房间不能为空");
        ObjectsUtil.requireNonNull(open, "动作不能为空");
        ObjectsUtil.requireNonNull(accountId, "用户不能为空");

        return R.success(socialVoiceRoomService.operateHostess(id, open, accountId));
    }

    @ApiOperation(value = "获取用户当前房间的权限(角色)", notes = "将返回用户拥有的角色(此角色只限于聊天室,自定义)")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "房间主键id", required = true),
        @ApiImplicitParam(name = "accountId", value = "平台用户id, 若为空，则取当前登录用户")
    })
    @GetMapping(value = "/getRoomUserRole")
    public R<SocialVoiceRoomUserRoleVo> getRoomUserRole(@RequestParam @NotNull Long id,
                                                        @RequestParam(value = "accountId", required = false) Long accountId){
        ObjectsUtil.requireNonNull(id, "房间不能为空");

        return R.success(socialVoiceRoomService.getRoomUserRole(id, accountId));
    }

}
